package kr.hkit.powerjava.chapter26.Young2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {
	
	private String driver;
	private String url;
	private String user;
	private String password;
	private static ConnectionFactory instance = new ConnectionFactory();

	
	public static Connection getConnection(){
		return instance.createConnection();
	}
	
	public ConnectionFactory() { //생성자
		loadProperties();
		
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			System.err.printf("%s%n", "MySQL Driver not Found");
			System.exit(0); //비정상종료인데... 
		}
		
	}


	private void loadProperties() {
		Properties prop = DBProperties.loadProperties("dbcon2.xml");
		driver = prop.getProperty("DRIVER");
		url = prop.getProperty("URL");
		user = prop.getProperty("USER");
		password = prop.getProperty("PASSWORD");
		
	}
	
	private Connection createConnection() {
		Connection con = null;
		
		try {
			con = DriverManager.getConnection(url, user, password);
			System.out.println("MySQL Connection Success");
		} catch (SQLException e) {
			System.err.printf("%s", "ERROR: Unable Connect to MySQL");
		}
		
		return con;
		
	}
}
